package com.sws.hm.controller;


import com.sws.common.BaseResponse;
import com.sws.common.StatusCode;
import com.sws.hm.dao.PidDao;
import com.sws.hm.dao.ProcessesinfoDao;
import com.sws.hm.dao.SysinfoDao;
import com.sws.hm.dao.portDao;
import com.sws.hm.dto.Pid;
import com.sws.hm.dto.port;
import com.sws.hm.entity.Processesinfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/pid")
@CrossOrigin
@Slf4j
public class PidController {

    @Autowired
    private SysinfoDao dao;

    @Autowired
    private PidDao piddao;

    @Autowired
    private ProcessesinfoDao processDao;

    @RequestMapping("findIpAll")
    public  BaseResponse findAll() {
        List<String> data = dao.findIpAll();
        int count=data.size();
        BaseResponse response=new BaseResponse(StatusCode.Success);
        response.setCount(count);
        response.setData(data);
        return response;
    }

    @RequestMapping("findpid")
    public  BaseResponse findpid() {
        List<String> data = dao.findIpAll();
        int count=data.size();
        BaseResponse response=new BaseResponse(StatusCode.Success);
        response.setCount(count);
        response.setData(data);
        return response;
    }

    @RequestMapping("process")
    public  BaseResponse process() {
        List<Processesinfo> data = processDao.SelectNewProcessesinfo();
        int count=data.size();
        BaseResponse response=new BaseResponse(StatusCode.Success);
        response.setCount(count);
        response.setData(data);
        return response;
    }


    @PostMapping("delete")
    public BaseResponse delete(int id,String ip,int pid) {

        int num=processDao.deleteById(ip,pid);
        processDao.deleteByIpAndPid(ip,pid);
        BaseResponse response=new BaseResponse(StatusCode.Success);
        response.setCount(num);
        return response;
    }

    @PostMapping("add")
    public BaseResponse add(String ip,int pid) {

        System.out.println(ip);
        System.out.println(pid);

        Pid E =new Pid();
        E.setIp(ip);
        E.setPid(pid);
        E.setIfdelte(0);
        int num=piddao.insert(E);
        System.out.println(num);
        Processesinfo processeByPod=new Processesinfo();
        processeByPod.setIp(ip);
        processeByPod.setPid(pid);
        processeByPod.setIfdelete(0);
        processeByPod.setIfsocket(2);
        processDao.insertProcessesinfo(processeByPod);

        System.out.println(processeByPod.toString());

        BaseResponse response=new BaseResponse(StatusCode.Success);
        response.setCount(num);
        return response;
    }
}
